﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class administrator_cart : System.Web.UI.Page
{
 
       dulieuDataContext _db=new dulieuDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadData();
            }
        }
        protected void GRV1_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            try
            {

                var st = e.CommandArgument.ToString();
                var ch = new char[] { '|' };
                var s = ThuVien.Lib.split(st, ch);
                var madonhang = s[0];
                switch (s[1])
                {
                    case "C":
                        Session.Add("madonhang", madonhang);
                        Response.Redirect("chi-tiet.aspx");
                        break;
                    case "U":
                        var orderRec = (from o in _db.Orders where o.Order_ID == int.Parse(madonhang) select o).FirstOrDefault();
                        if (orderRec.Status == null)
                            orderRec.Status = false;
                        orderRec.Status = !orderRec.Status;
                        _db.SubmitChanges();
                        Response.Redirect("cart.aspx");
                        break;
                }
            }
            catch
            {
            }
        }
        protected void LoadData()
        {
            DataTable dtSource = new DataTable();
            dtSource.Columns.Add(new DataColumn("Order_ID", typeof(System.Int32)));
            dtSource.Columns.Add(new DataColumn("CreateTime", typeof(System.DateTime)));
            dtSource.Columns.Add(new DataColumn("Dadatmua", typeof(System.String)));

            var orderSource = (from o in _db.Orders select new { o.Order_ID, o.CreateTime, o.Status }).Distinct(); ;
            foreach (var item in orderSource)
            {
                if (item.Status == true)
                    dtSource.Rows.Add(item.Order_ID, item.CreateTime, "Hủy");
                else
                    dtSource.Rows.Add(item.Order_ID, item.CreateTime, "Đã mua");
            }
            GRVORDER.DataSource = dtSource;
            GRVORDER.DataBind();
            //GRVORDER.DataSource = (from o in _db.Orders select new {o.Order_ID,o.CreateTime}).Distinct();
            //GRVORDER.DataBind();
        }
   
        protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='none'; this.style.backgroundColor='gray'";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='white'";
            }

        }
        //protected void XoaClick(object sender,EventArgs e)
        //{
        //    foreach (GridViewRow gridViewRow in GRVORDER.Rows)
        //    {
        //        var chkxoa = (CheckBox) gridViewRow.FindControl("chkxoa");
        //        if (!chkxoa.Checked) continue;
        //        var lblxoa =(Label)gridViewRow.FindControl("lblxoa");
        //        var dathang = (_db.Orders.Where(o => o.Order_ID == Convert.ToInt16(lblxoa.Text)));
        //        var chitietdathang = (_db.OrderDetails.Where(o => o.Order_ID == Convert.ToInt16(lblxoa.Text))).FirstOrDefault();
        //        _db.Orders.DeleteAllOnSubmit(dathang);
        //        if (chitietdathang != null) _db.OrderDetails.DeleteOnSubmit(chitietdathang);
        //        _db.SubmitChanges();
        //        LoadData();
        //    }
        //}
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GRVORDER.PageIndex = e.NewPageIndex;
            LoadData();
        }
    }
